package com.mxx.democharts2.web.controller;

import com.mxx.democharts2.dto.YuancheDto2;
import com.mxx.democharts2.support.RestResponse;
import com.mxx.democharts2.vo.YuancheQuery;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/yuanche2")
public class YuancheController2 {


    @GetMapping("/list")
    public RestResponse list(Integer page, Integer limit, YuancheQuery yuancheQuery){

        List<YuancheDto2> dataList = new ArrayList<>();
        // 读取文本
        // TODO 改成HDFS版本的，这段代码要移到DAO里
        try {

            BufferedReader bf=new BufferedReader(new FileReader(ResourceUtils.getFile("classpath:yuancheData/yuanche_7.txt")));
            String str;
            while ((str = bf.readLine()) != null) {
//                System.out.println(str);
                String[] lineData = str.split(",");
                for(int i=0; i<lineData.length; ++i){
                    if(lineData[i].equals("null")){
                        lineData[i] = null;
                    }
                }
                YuancheDto2 yuancheDto2 = new YuancheDto2();
                yuancheDto2.setNewCode(lineData[0]);
                yuancheDto2.setOldCode(lineData[1]);
                yuancheDto2.setPartName(lineData[2]);
                yuancheDto2.setBanCode(lineData[3]);
                yuancheDto2.setMouthCount(lineData[4]);
                // y_2019_1
                List<String> y_2019_1 = new ArrayList<>();
                for(int i=5; i<=16; ++i){
                    y_2019_1.add(lineData[i]);
                }
                yuancheDto2.setY_2019_1(y_2019_1);

                // y_2019_2
                List<String> y_2019_2 = new ArrayList<>();
                for(int i=17; i<=24; ++i){
                    y_2019_2.add(lineData[i]);
                }
                yuancheDto2.setY_2019_2(y_2019_2);


                // TODO 其他属性待填充
//
              dataList.add(yuancheDto2);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new RestResponse(dataList);
    }
}
